<template>
  <el-card>
    <div slot="header" style="line-height: 40px">
      <h1>添加地址</h1>
    </div>
    <el-form
      ref="addresssForm"
      :rules="rules"
      label-width="80px"
      :model="addresssForm"
    >
      <el-form-item label="ID" v-show="false">
        <el-input v-model="addresssForm.id"></el-input>
      </el-form-item>
      <el-form-item label="用户ID" prop="uid">
        <el-input v-model="addresssForm.uid"></el-input>
      </el-form-item>
      <el-form-item label="用户地址" prop="address">
        <el-input v-model="addresssForm.address"></el-input>
      </el-form-item>
      <el-form-item label="用户电话" prop="aphone">
        <el-input v-model.trim="addresssForm.aphone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import addresssApi from '@/http/addressApi';

export default {
  data() {
    let checkuid = (rule, value, callback) => {
      addresssApi.checkuid(value).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          if (res.data.msg) {
            callback();
          } else {
            callback(new Error());
          }
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: 'error',
          });
        }
      });
    };
    return {
      addresssForm: {
        uid:'',
        aphone:'',
        address:'',
      },
      rules: {
        uid: [
          {
            required: true,
            message: '请输入用户ID',
            trigger: 'blur',
          },
          {
            validator: checkuid,
            message: '没有这个用户ID',
            trigger: 'blur',
          },
        ],
        address: [
          {
            required: true,
            message: '请输入用户地址',
            trigger: 'blur',
          },
        ],
        aphone: [
          {
            required: true,
            message: '请输入用户电话',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  created() {
    this.getEditObj();
  },
  methods: {
    onSubmit() {
      this.$refs['addresssForm'].validate(valid => {
        if (valid) {
          console.log('submit!', this.addresssForm);
          addresssApi.save(this.addresssForm).then(res => {
            console.log(res);
            if (res.data.code == 200) {
              this.$router.replace('/account/address.html');
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: 'error',
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false; //阻止表单提交
        }
      });
    },
    onReset() {
      this.$refs['addresssForm'].resetFields();
    },
    getEditObj() {
      let id = this.$route.params.id;
      if (id) {
        addresssApi.edit(id).then(res => {
          if (res.data.code == 200) {
            this.addresssForm = res.data.data;
          }
        });
      }
    },
  },
};
</script>

<style></style>
